package com.castle.fortress.admin.flowable.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.flowable.dto.FlowableCommentExtendDto;
import com.castle.fortress.admin.flowable.entity.FlowableCommentExtendEntity;
import com.castle.fortress.admin.flowable.service.FlowableCommentExtendService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程评论扩展表 api 控制器
 *
 * @author castle
 * @since 2023-12-01
 */
@Api(tags="流程评论扩展表api管理控制器")
@RestController
@RequestMapping("/api/flowable/flowableCommentExtend")
public class ApiFlowableCommentExtendController {
    @Autowired
    private FlowableCommentExtendService flowableCommentExtendService;


    /**
     * 流程评论扩展表的分页展示
     * @param flowableCommentExtendDto 流程评论扩展表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="流程评论扩展表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("流程评论扩展表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<FlowableCommentExtendDto>> pageFlowableCommentExtend(FlowableCommentExtendDto flowableCommentExtendDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<FlowableCommentExtendDto> page = new Page(pageIndex, pageSize);

        IPage<FlowableCommentExtendDto> pages = flowableCommentExtendService.pageFlowableCommentExtend(page, flowableCommentExtendDto);
        return RespBody.data(pages);
    }

    /**
     * 流程评论扩展表保存
     * @param flowableCommentExtendDto 流程评论扩展表实体类
     * @return
     */
    @CastleLog(operLocation="流程评论扩展表-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("流程评论扩展表保存")
    @PostMapping("/save")
    public RespBody<String> saveFlowableCommentExtend(@RequestBody FlowableCommentExtendDto flowableCommentExtendDto){
        if(flowableCommentExtendDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            FlowableCommentExtendEntity flowableCommentExtendEntity = ConvertUtil.transformObj(flowableCommentExtendDto,FlowableCommentExtendEntity.class);
        if(flowableCommentExtendService.save(flowableCommentExtendEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 流程评论扩展表编辑
     * @param flowableCommentExtendDto 流程评论扩展表实体类
     * @return
     */
    @CastleLog(operLocation="流程评论扩展表-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("流程评论扩展表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateFlowableCommentExtend(@RequestBody FlowableCommentExtendDto flowableCommentExtendDto){
        if(flowableCommentExtendDto == null || flowableCommentExtendDto.getId() == null || flowableCommentExtendDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            FlowableCommentExtendEntity flowableCommentExtendEntity = ConvertUtil.transformObj(flowableCommentExtendDto,FlowableCommentExtendEntity.class);
        if(flowableCommentExtendService.updateById(flowableCommentExtendEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 流程评论扩展表删除
     * @param ids 流程评论扩展表id集合
     * @return
     */
    @CastleLog(operLocation="流程评论扩展表-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("流程评论扩展表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteFlowableCommentExtend(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(flowableCommentExtendService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 流程评论扩展表详情
     * @param id 流程评论扩展表id
     * @return
     */
    @CastleLog(operLocation="流程评论扩展表-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("流程评论扩展表详情")
    @GetMapping("/info")
    public RespBody<FlowableCommentExtendDto> infoFlowableCommentExtend(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            FlowableCommentExtendEntity flowableCommentExtendEntity = flowableCommentExtendService.getById(id);
            FlowableCommentExtendDto flowableCommentExtendDto = ConvertUtil.transformObj(flowableCommentExtendEntity,FlowableCommentExtendDto.class);
        return RespBody.data(flowableCommentExtendDto);
    }


}
